var cartData = [];
var sumPrice = 0;
layui.use(['form', 'layer', 'carousel', 'element', 'jquery'], function () {
    var form = layui.form;
    var $ = layui.jquery,
        layer = layui.layer;

});
layui.use(['form', 'jquery'], function () {
    var form = layui.form;
    var $ = layui.jquery;
    /**
     * 全选
     */
    form.on('checkbox(chooseAll)', function (data) {
        var child = $('input[name=ids]');
        child.each(function (index, item) {
            item.checked = data.elem.checked;
        })
        form.render('checkbox')
        getSum();
    })

    /**
     * 单选
     */

    /**
     * todo 功能完善
     */
    form.on('checkbox(chooseOne)', function () {
        var num = 0;
        getSum();
        var sum = getCartData().length;
        console.log(sum)
        console.log($('input[name=ids]:checked').size())
        if ($('input[name=ids]:checked').size() === sum) {
            console.log(1111111111)
            $('input[name=check]').eq(0).attr("checked", "false")
            // form.render()
            console.log("显示全选" + $('#checkAll').eq(0).html())
        } else {
            $('input[name=check]').eq(0).attr("checked", "true")
            console.log("不全选" + $('input[name=check]').eq(0).html())
        }
        form.render();
    })

})

/**
 * 计算选中的商品的总价
 */
function getSum() {
    var that = this;
    var cartData1 = getCartData();
    var sum = 0;
    var selectd = $('input[name=ids]:checked');
    selectd.each(function (index, item) {
        var id = $(item).val();
        for (let i = 0; i < cartData1.length; i++) {
            var cart = cartData1[i];
            if (cart.commid === id) {
                sum += cart.thinkmoney
            }
            that.sumPrice = sum;
        }
    })
    $('#price').text("￥  " + sum)
}


function buy() {
    var map = new Object();
    console.log(sumPrice)
    if (this.sumPrice == 0) {
        layer.msg("您还未选购商品", {
            time: 1000,
            icon: 5,
            offset: '100px'
        });
        return;
    }
    var idList = [];
    var selectd = $('input[name=ids]:checked');
    selectd.each(function (index, item) {
        var id = $(item).val();
        idList.push(id)
    })
    map['sumPrice'] = sumPrice
    map['idList'] = idList
    console.log(map)
    $.ajax({
        url: basePath + '/user/checkCart'
        , data: JSON.stringify(map)
        , contentType: 'application/json;charset=UTF-8'
        , type: 'post'
        , dataType: 'json'
        , beforeSend: function () {
            layer.load(1, { //icon支持传入0-2
                content: '转跳中...',
                success: function (layero) {
                    layero.find('.layui-layer-content').css({
                        'padding-top': '39px',
                        'width': '60px'
                    });
                }
            });
        }, complete: function () {
            layer.closeAll('loading');
        }, success: function (data) {
            if (data.status == 500) {
                layer.msg("系统错误", {
                    time: 1000,
                    icon: 2,
                    offset: '100px'
                });
            } else if (data.status == 201) {
                layer.msg(data.message, {
                    time: 1000,
                    icon: 2,
                    offset: '100px'
                });
            } else if (data.status == 200) {

                // window.open(basePath + "/alipay/pay?subject=12156564&traceNo=121554&totalAmount=1000")
                // window.open("http://www.baidu.com")
                window.location.href=basePath+"/user/orderList"
            }
        }, error: function () {
            layer.msg("系统错误", {
                time: 1000,
                icon: 2,
                offset: '100px'
            });
        }
    })
}

window.onload = function () {
    new Vue({
        el: '#product_cart',
        data() {
            return {
                cartListData: []
            }
        }, mounted() {
            window.getCartData = this.getCartData;
            this.getCartList();
            this.$nextTick(function () {
                /**
                 * 重新渲染
                 */
                layui.use(['form'], function () {
                    var form = layui.form;
                    form.render('checkbox')
                })
            })
        }, methods: {
            getCartList: function () {
                let that = this;
                $.ajax({
                    /**
                     * 为使渲染后checkbox可用 需关闭异步
                     */
                    async: false,
                    url: basePath + "/user/getCartList/" + userid,
                    data: '',
                    type: 'get',
                    contentType: 'application/json;charset=UTF-8',
                    dataType: 'json',
                    success: function (res) {
                        if (res.status === 200) {
                            that.cartListData = res.data;
                            this.cartData = res.data;
                        }
                    }, error: function () {
                        layer.msg("系统错误", {
                            time: 1000,
                            icon: 5,
                            offset: '100px'
                        });
                    }, complete: function () {
                        layer.closeAll('loading');
                    },
                })
            },
            getCartData: function () {
                /*把局部变量转换出去*/
                return this.cartListData;
            }
        }
    })
}
